﻿using MxWeiXinPF.API.Payment.Tenpay;
using MxWeiXinPF.Common;
using MxWeiXinPF.Web.UI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.weixin.jiaoshuifei
{
    public partial class BindList : WeiXinPage
    {
        string key = "";
        string wxusername = "";
        public int wid = 0;
        string apiurl = "";


        public List<Model.BindRecord> list = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            wid = MyCommFun.RequestWid();
            string thisUrl = MyCommFun.getWebSite() + "/weixin/jiaoshuifei/bindlist.aspx?wid=" + wid;
            OAuth2BaseProc(wid, thisUrl);


            if (wid == 0 || openid == "")
            {
                Response.Redirect("/weixin/jiaoshuifei/index.aspx?wid=" + wid);
            }

            string msg = string.Empty;
             
             MxWeiXinPF.BLL.shuifei_baseinfo _baseInfoBLL = new BLL.shuifei_baseinfo();
             Model.shuifei_baseinfo baseinfoModel = _baseInfoBLL.GetModelByWid(Convert.ToInt32(wid));
             if (baseinfoModel != null)
             {
                 wxusername = baseinfoModel.api_UserName;
                 key = baseinfoModel.api_Key;
                 apiurl = baseinfoModel.api_url;

             }
             list = BindRecordList(wxusername, key, apiurl, openid, "", out msg);

             if (list == null || list.Count <= 0)
             {
                 Response.Redirect("/weixin/jiaoshuifei/bindone.aspx?wid=" + wid);

             }
        }
        /// <summary>
        /// 根据openid获取用户绑定列表
        /// </summary>
        /// <param name="wxusername"></param>
        /// <param name="openid"></param>
        /// <param name="extend"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<Model.BindRecord> BindRecordList(string wxusername, string key,string apiurl,string openid, string extend, out string msg)
        {
            msg = string.Empty;
            string tradeNo = "1009";
            string datetimeStr = DateTime.Now.ToString("yyyyMMddhhmmssfff");
            StringBuilder strBuf = new StringBuilder();
            strBuf.Append(wxusername);
            strBuf.Append(tradeNo);
            strBuf.Append(openid);
            strBuf.Append(extend);
            strBuf.Append(datetimeStr);
            strBuf.Append(key);
            string sign = MD5Util.GetMD5(strBuf.ToString(), Encoding.UTF8.ToString()).ToUpper();
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("extend", extend);
            sParaTemp.Add("openid", openid);
            sParaTemp.Add("randomstr", datetimeStr);
            sParaTemp.Add("tradecode", tradeNo);
            sParaTemp.Add("username", wxusername);
            sParaTemp.Add("sign", sign);

            string para = JsonConvert.SerializeObject(sParaTemp);
            jsfservice.WXWaterServiceSoapClient jsfservice = new jsfservice.WXWaterServiceSoapClient("WXWaterServiceSoap12", apiurl);

            string returnMsg = jsfservice.wxProgress(para);
            if (string.IsNullOrEmpty(returnMsg))
            {
                msg = "returnMsg=null";
                return null;
            }
            Model.JSFResultModel resultModel = JsonConvert.DeserializeObject<Model.JSFResultModel>(returnMsg);
            if (resultModel == null)
            {
                msg = "resultModel=null";
                return null;
            }
  
            if (resultModel.rtncode != "OK")
            {
                msg = resultModel.rtnmsg;
                return null;
            }
            if (resultModel.recordList == null || resultModel.recordList.Count <= 0)
            {
                msg = "没有绑定的户号";
                return null;
            }

            return resultModel.recordList;
        }
    }
}